package io.renren.service;

import io.renren.dao.EboardStuCardRecordDao;
import io.renren.dao.GeneralClassDao;
import io.renren.dao.GeneralStudentDao;
import io.renren.dao.SysUserRoleDao;
import io.renren.entity.EboardStatistic;
import io.renren.entity.EboardStuCardRecordEntity;
import io.renren.entity.GeneralClassEntity;
import io.renren.entity.GeneralStudentEntity;
import io.renren.util.BootstrapPageBean;
import io.renren.util.DateUtil;
import io.renren.util.ResultJson;
import io.renren.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.Map.Entry;

/**
 * Created by 94504 on 2017/7/20.
 */
@Service
public class EboardStatisticService
{
    @Autowired
    private EboardStuCardRecordDao cardRecordDao;
    @Autowired
    private GeneralStudentDao studentDao;
    @Autowired
    private GeneralClassDao classDao;
    @Autowired
    private SysUserRoleDao userRoleDao;


    public BootstrapPageBean pageStuCardRecord(BootstrapPageBean pageBean, String format, EboardStatistic es, Long roleId)
    {
        List<EboardStuCardRecordEntity> conditionList = new ArrayList<>();
        List<EboardStatistic> resultList = new ArrayList<>();
        ResultJson resultJson = new ResultJson();
        Integer late = 0;
        Integer early = 0;
        Integer inSchool = 0;
        Integer checked = 0;    // 实到人数

        /*
         * 查询条件
         */
        String types;
        String startDate = es.getStartDate();
        String endDate = es.getEndDate();
        String classIdStrs = "";

        StringBuilder sb = new StringBuilder();
        if (es.getComeLate() != null)
        {
            if (es.getComeLate())
                sb.append("2,");
            else
                sb.append("0,1,");
        }

        if (es.getLeaveEarly() != null)
        {
            if (es.getLeaveEarly())
                sb.append("4,");
            else
                sb.append("3,");
        }

        types = sb.toString();
        if ((types.length() > 0) && types.endsWith(","))
            types = types.substring(0, types.length() - 1);

        if (StringUtils.isBlank(startDate) && StringUtils.isNotBlank(endDate))
        {
            startDate = endDate;
        }
        else if (StringUtils.isBlank(endDate) && StringUtils.isNotBlank(startDate))
        {
            endDate = startDate;
        }
        else if (StringUtils.isBlank(endDate) && StringUtils.isBlank(startDate))
        {
            startDate = DateUtil.getNowDateString("yyyy-MM-dd");
            endDate = startDate;
        }

        endDate = DateUtil.getNextDay(endDate, "1");

        List<GeneralClassEntity> classEntityList;

        if (es.getClassId() == null)
        {
            if (Objects.equals(roleId, Long.parseLong("2")))    // 学校管理员
                classEntityList = classDao.queryBySchoolId(ShiroUtils.getUserEntity().getSchoolId());
            else
                classEntityList = classDao.queryByRole(ShiroUtils.getUserEntity().getUserId(), ShiroUtils.getUserEntity().getSchoolId());

            StringBuilder classIdSB = new StringBuilder();
            if (classEntityList != null)
            {
                for (GeneralClassEntity classEntity : classEntityList)
                {
                    classIdSB.append(classEntity.getId());
                    classIdSB.append(",");
                }
            }

            if (classIdSB.length() > 0)
                classIdStrs = classIdSB.substring(0, classIdSB.length() - 1);
        }


        conditionList = cardRecordDao.queryForStatistic(types, classIdStrs, startDate, endDate
                , pageBean.getLimit(), pageBean.getOffset(), pageBean.getSort(), pageBean.getOrder());


        if (conditionList != null)
        {
            // key为checkTime的map
            Map<String, List> checkTimeMap = new HashMap<>();
            for (EboardStuCardRecordEntity record_1 : conditionList)
            {
                String key_1 = DateUtil.formatToStr(record_1.getCheckTime(), format);
                if (checkTimeMap.containsKey(key_1))
                {
                    List<EboardStuCardRecordEntity> list = checkTimeMap.get(key_1);
                    list.add(record_1);
                    checkTimeMap.put(key_1, list);
                }
                else
                {
                    List<EboardStuCardRecordEntity> list = new ArrayList<>();
                    list.add(record_1);
                    checkTimeMap.put(key_1, list);
                }
            }

            Set<Entry<String, List>> entrySet_1 = checkTimeMap.entrySet();
            for (Entry<String, List> entry_1 : entrySet_1)
            {
                // 某个日期所有数据
                List<EboardStuCardRecordEntity> list = entry_1.getValue();

                Map<String, List> stuNoMap = new HashMap<>();

                for (EboardStuCardRecordEntity record_2 : list)
                {
                    String keyForStuNo = record_2.getStuNo();
                    if (stuNoMap.containsKey(keyForStuNo))
                    {
                        List<EboardStuCardRecordEntity> list_2 = stuNoMap.get(keyForStuNo);
                        list_2.add(record_2);
                        stuNoMap.put(keyForStuNo, list_2);
                    }
                    else
                    {
                        List<EboardStuCardRecordEntity> list_2 = new ArrayList<>();
                        list_2.add(record_2);
                        stuNoMap.put(keyForStuNo, list_2);
                    }

                }   // iterate checkTimeMapKeyList end

                if (stuNoMap.size() != 0)
                {

                    Set<Entry<String, List>> entrSet_2 = stuNoMap.entrySet();
                    for (Entry<String, List> entry_2 : entrSet_2)
                    {
                        EboardStatistic eStatistic = new EboardStatistic();
                        /*
                         * 初始化参数
                         */
                        Date checkInTime = null;
                        Date checkOutTime = null;
                        String checkInStatus = "-1";
                        String checkOutStatus = "-1";
                        String stuNo = "";
                        String stuNumber = "";  //学号
                        String stuName = "";
                        String className = "";


                        List<EboardStuCardRecordEntity> listForStuNoMap = entry_2.getValue();

                        Date flagDate = null;
                        String flagStatus = "-1";

                        // 最后只有一条数据
                        for (EboardStuCardRecordEntity record : listForStuNoMap)
                        {
                            if (flagDate == null)
                            {
                                flagDate = record.getCheckTime();
                                flagStatus = record.getCheckType().toString();

                            }

                            if (flagDate.before(record.getCheckTime()))
                            {
                                flagDate = record.getCheckTime();
                                flagStatus = record.getCheckType().toString();
                            }

                            stuNo = record.getStuNo();

                            if (StringUtils.isNotBlank(stuNo))
                            {
                                GeneralStudentEntity generalStudentEntity = studentDao.selectOne(new GeneralStudentEntity(stuNo));
                                if (generalStudentEntity != null)
                                {
                                    stuName = generalStudentEntity.getName();
                                    stuNumber = generalStudentEntity.getStuNumber();
                                    className = classDao.selectByPrimaryKey(generalStudentEntity.getClassId()).getName();
                                }

                            }

                            // 0 正常签到 1早于正常签到时间 2晚于正常签到时间
                            if (record.getCheckType() == 0 || record.getCheckType() == 1 || record.getCheckType() == 2)
                            {
                                if (checkInTime == null) // 第一次签到
                                {
                                    checkInTime = record.getCheckTime();
                                    checkInStatus = record.getCheckType().toString();
                                    checked++;
                                }
                                else    // 多次签
                                {
                                    // 当有多条数据时，取时间最早的
                                    if (checkInTime.after(record.getCheckTime()))
                                    {
                                        checkInTime = record.getCheckTime();
                                        checkInStatus = record.getCheckType().toString();
                                    }
                                }
                            }
                            else if (record.getCheckType() == 3 || record.getCheckType() == 4)   // 签退时间
                            {
                                if (checkOutTime == null) // 第一次签退
                                {
                                    checkOutTime = record.getCheckTime();
                                    checkOutStatus = record.getCheckType().toString();
                                }
                                else    // 多次签退
                                {
                                    if (checkOutTime.before(record.getCheckTime()))
                                    {
                                        checkOutTime = record.getCheckTime();
                                        checkOutStatus = record.getCheckType().toString();
                                    }
                                }
                            }

                            // 签到时间和签到状态
                            if (checkInTime != null)
                            {
                                eStatistic.setCheckInStatus(checkInStatus);
                                eStatistic.setCheckInTime(DateUtil.formatToStr(checkInTime, "yyyy-MM-dd HH:mm:ss"));
                                //map.put("checkInStatus", checkInStatus);
                                //map.put("checkInTime", DateUtil.formatToStr(checkInTime, "yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {
                                eStatistic.setCheckInStatus(checkInStatus);
                                //map.put("checkInStatus", checkInStatus);
                                eStatistic.setCheckInTime("未签到");
                                //map.put("checkInTime", "未签到");
                            }

                            // 签退
                            if (checkOutTime != null)
                            {
                                eStatistic.setCheckOutStatus(checkOutStatus);
                                eStatistic.setCheckOutTime(DateUtil.formatToStr(checkOutTime, "yyyy-MM-dd HH:mm:ss"));
                                //map.put("checkOutStatus", checkOutStatus);
                                //map.put("checkOutTime", DateUtil.formatToStr(checkOutTime, "yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {
                                eStatistic.setCheckOutStatus(checkOutStatus);
                                eStatistic.setCheckOutTime("未签退");
//                                map.put("checkOutStatus", checkOutStatus);
//                                map.put("checkOutTime", "未签退");
                            }

                            //resultList.add(record);

                        }   // iterate listForStuNoMap end


                        // 通过最后一次刷卡状态码来判断学生是否在校
                        if (StringUtils.equals("0", flagStatus) || StringUtils.equals("1", flagStatus) || StringUtils.equals("2", flagStatus))
                        {
                            eStatistic.setInSchool(true);
                            eStatistic.setCheckOutTime("未签退");
                            eStatistic.setCheckOutStatus("-1");
                        }
                        else
                        {
                            eStatistic.setInSchool(false);
                        }

                        // 判断是否到校过
                        if (!StringUtils.equals("-1", eStatistic.getCheckInStatus()))
                            inSchool++;

                        eStatistic.setStuNo(stuNo);
                        eStatistic.setStuName(stuName);
                        eStatistic.setStuNumber(stuNumber);
                        eStatistic.setClassName(className);
//                        map.put("stuNo", stuNo);
//                        map.put("stuName", stuName);
//                        map.put("stuNumber", stuNumber);
//                        map.put("className", className);
                        //Map aMap = new HashMap(map);
                        resultList.add(eStatistic);

                    }   // iterate stuNoMap end

                }   // iterate checkTimeMap end
            }   // if entrySet != null end
        } // if conditionList != null end


        if (resultList.size() != 0)
        {
            for (EboardStatistic eboardStatistic : resultList)
            {
                if (StringUtils.equals("2", eboardStatistic.getCheckInStatus()))
                {
                    eboardStatistic.setComeLate(true);
                    late++;
                }
                else
                {
                    eboardStatistic.setComeLate(false);
                }

                if (StringUtils.equals("4", eboardStatistic.getCheckOutStatus()))
                {
                    eboardStatistic.setLeaveEarly(true);
                    early++;
                }
                else
                {
                    eboardStatistic.setLeaveEarly(false);
                }


                /*
                if (!StringUtils.equals("-1", (String) map.get("checkInStatus")) //已签到
                        && StringUtils.equals("-1", (String) map.get("checkOutStatus"))) //未签退
                {
                    eboardStatistic.setInSchool(true); // 在校
                    inSchool++;
                }
                else
                {
                    eboardStatistic.setInSchool(false);
                }
                */
            }
        }


        List<EboardStatistic> finalList = new ArrayList<>();
        if (es.getInSchool() != null)
        {
            for (EboardStatistic eboardStatistic : resultList)
            {
                if (es.getInSchool() != null && Objects.equals(es.getInSchool(), eboardStatistic.getInSchool()))
                    finalList.add(eboardStatistic);
            }
        }
        else
        {
            finalList = resultList;
        }

        Map<String, Object> map2 = new HashMap<>();

        Integer studentCount = 0;
        Integer allCount = 0;

        if (classIdStrs != null)
            studentCount = studentDao.queryCountByClassIds(classIdStrs);
        else if (es.getClassId() != null)
        {
            GeneralStudentEntity student = new GeneralStudentEntity();
            student.setClassId(es.getClassId());
            studentCount = studentDao.selectCount(student);
        }

        //TODO 应到人数的处理
        Integer days = DateUtil.getTwoDay(endDate, startDate) + 1;
        allCount = studentCount * days;

        map2.put("comeLateCount", late);
        map2.put("leaveEarlyCount", early);
        map2.put("allCount", allCount);
        map2.put("inSchoolCount", checked);

        //resultJson.setData(finalList);
        //resultJson.setOtherData(map2);

        pageBean.setRows(finalList);
        pageBean.setTotal(cardRecordDao.queryCountForStatistic(types, classIdStrs, startDate, endDate));
        pageBean.setData(map2);

        return pageBean;

    }

    public ResultJson getStuCardRecordList(BootstrapPageBean pageBean, String format, EboardStatistic es, Long roleId)
    {
        ResultJson resultJson = new ResultJson();
        List<EboardStatistic> resultList = new ArrayList<>();
        Integer late = 0;
        Integer early = 0;
        Integer inSchool = 0;
        Integer checked = 0;    // 实到人数

        EboardStuCardRecordEntity mmp = new EboardStuCardRecordEntity();
        mmp.setClassId(es.getClassId());
        StringBuilder sb = new StringBuilder();
        if (es.getComeLate() != null)
        {
            if (es.getComeLate())
                sb.append("2,");
            else
                sb.append("0,1,");
        }

        if (es.getLeaveEarly() != null)
        {
            if (es.getLeaveEarly())
                sb.append("4,");
            else
                sb.append("3,");
        }

        String types = sb.toString();
        if ((types.length() > 0) && types.endsWith(","))
            types = types.substring(0, types.length() - 1);

        Example example = new Example(EboardStuCardRecordEntity.class);

        String conditionStr = "";
        String startDate = es.getStartDate();
        String endDate = es.getEndDate();
        // 符合添加的classId
        String classIdStrs = "";

        if (StringUtils.isBlank(startDate) && StringUtils.isNotBlank(endDate))
        {
            startDate = endDate;
        }
        else if (StringUtils.isBlank(endDate) && StringUtils.isNotBlank(startDate))
        {
            endDate = startDate;
        }
        else if (StringUtils.isBlank(endDate) && StringUtils.isBlank(startDate))
        {
            startDate = DateUtil.getNowDateString("yyyy-MM-dd");
            endDate = startDate;
        }

        if ((types.length() != 0))
            conditionStr += " check_type in (" + types + ") AND";

        if (StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate))
            conditionStr += " check_time BETWEEN '" + startDate + "' AND '" + DateUtil.getNextDay(endDate, "1", "yyyy/MM/dd") + "'";

        /*
            如果es.getClassId不为空要就只判断classId=es.getClassId, 否则判断classId in(XXXX)
         */
        List<GeneralClassEntity> classEntityList = new ArrayList<>();

        if (es.getClassId() == null)
        {
            if (Objects.equals(roleId, Long.parseLong("2")))    // 学校管理员
                classEntityList = classDao.queryBySchoolId(ShiroUtils.getUserEntity().getSchoolId());
            else
                classEntityList = classDao.queryByRole(ShiroUtils.getUserEntity().getUserId(), ShiroUtils.getUserEntity().getSchoolId());

            StringBuilder classIdSB = new StringBuilder();
            if (classEntityList != null)
            {
                for (GeneralClassEntity classEntity : classEntityList)
                {
                    classIdSB.append(classEntity.getId());
                    classIdSB.append(",");
                }
            }

            if (classIdSB.length() > 0)
                classIdStrs = classIdSB.substring(0, classIdSB.length() - 1);

            // 老师有相关班级
            if (StringUtils.isNotBlank(classIdStrs))
                conditionStr += " AND class_id IN(" + classIdStrs + ")";
            else
                return null;
        }
        else
        {
            conditionStr += " AND class_id = " + es.getClassId();
        }

        if (StringUtils.isNotBlank(conditionStr))
            conditionStr += "( " + conditionStr + " ) ";

        if (StringUtils.isNotBlank(pageBean.getSort()))
        {
            conditionStr += " ORDER BY " + pageBean.getSort();

            if (StringUtils.isNotBlank(pageBean.getOrder()))
                conditionStr += " " + pageBean.getOrder();
        }
        if (pageBean.getLimit() != null)
            conditionStr += " limit " + pageBean.getOffset() + "," + pageBean.getLimit();

        List<EboardStuCardRecordEntity> conditionList;

        example.createCriteria().andCondition(conditionStr);

        conditionList = cardRecordDao.selectByExample(example);
        /*
        if (conditionStr.length() != 0)
            conditionList = cardRecordDao.selectByExample(example);
        else
            conditionList = cardRecordDao.selectAll();
        */

        if (conditionList != null)
        {
            // key为checkTime的map
            Map<String, List> checkTimeMap = new HashMap<>();
            for (EboardStuCardRecordEntity record_1 : conditionList)
            {
                String key_1 = DateUtil.formatToStr(record_1.getCheckTime(), format);
                if (checkTimeMap.containsKey(key_1))
                {
                    List<EboardStuCardRecordEntity> list = checkTimeMap.get(key_1);
                    list.add(record_1);
                    checkTimeMap.put(key_1, list);
                }
                else
                {
                    List<EboardStuCardRecordEntity> list = new ArrayList<>();
                    list.add(record_1);
                    checkTimeMap.put(key_1, list);
                }
            }

            Set<Entry<String, List>> entrySet_1 = checkTimeMap.entrySet();
            for (Entry<String, List> entry_1 : entrySet_1)
            {
                // 某个日期所有数据
                List<EboardStuCardRecordEntity> list = entry_1.getValue();

                Map<String, List> stuNoMap = new HashMap<>();

                for (EboardStuCardRecordEntity record_2 : list)
                {
                    String keyForStuNo = record_2.getStuNo();
                    if (stuNoMap.containsKey(keyForStuNo))
                    {
                        List<EboardStuCardRecordEntity> list_2 = stuNoMap.get(keyForStuNo);
                        list_2.add(record_2);
                        stuNoMap.put(keyForStuNo, list_2);
                    }
                    else
                    {
                        List<EboardStuCardRecordEntity> list_2 = new ArrayList<>();
                        list_2.add(record_2);
                        stuNoMap.put(keyForStuNo, list_2);
                    }

                }   // iterate checkTimeMapKeyList end

                if (stuNoMap.size() != 0)
                {

                    Set<Entry<String, List>> entrSet_2 = stuNoMap.entrySet();
                    for (Entry<String, List> entry_2 : entrSet_2)
                    {
                        EboardStatistic eStatistic = new EboardStatistic();
                        /*
                         * 初始化参数
                         */
                        Date checkInTime = null;
                        Date checkOutTime = null;
                        String checkInStatus = "-1";
                        String checkOutStatus = "-1";
                        String stuNo = "";
                        String stuNumber = "";  //学号
                        String stuName = "";
                        String className = "";


                        List<EboardStuCardRecordEntity> listForStuNoMap = entry_2.getValue();

                        Date flagDate = null;
                        String flagStatus = "-1";

                        // 最后只有一条数据
                        for (EboardStuCardRecordEntity record : listForStuNoMap)
                        {
                            if (flagDate == null)
                            {
                                flagDate = record.getCheckTime();
                                flagStatus = record.getCheckType().toString();

                            }

                            if (flagDate.before(record.getCheckTime()))
                            {
                                flagDate = record.getCheckTime();
                                flagStatus = record.getCheckType().toString();
                            }

                            stuNo = record.getStuNo();

                            if (StringUtils.isNotBlank(stuNo))
                            {
                                GeneralStudentEntity generalStudentEntity = studentDao.selectOne(new GeneralStudentEntity(stuNo));
                                if (generalStudentEntity != null)
                                {
                                    stuName = generalStudentEntity.getName();
                                    stuNumber = generalStudentEntity.getStuNumber();
                                    className = classDao.selectByPrimaryKey(generalStudentEntity.getClassId()).getName();
                                }

                            }

                            // 0 正常签到 1早于正常签到时间 2晚于正常签到时间
                            if (record.getCheckType() == 0 || record.getCheckType() == 1 || record.getCheckType() == 2)
                            {
                                if (checkInTime == null) // 第一次签到
                                {
                                    checkInTime = record.getCheckTime();
                                    checkInStatus = record.getCheckType().toString();
                                    checked++;
                                }
                                else    // 多次签
                                {
                                    // 当有多条数据时，取时间最早的
                                    if (checkInTime.after(record.getCheckTime()))
                                    {
                                        checkInTime = record.getCheckTime();
                                        checkInStatus = record.getCheckType().toString();
                                    }
                                }
                            }
                            else if (record.getCheckType() == 3 || record.getCheckType() == 4)   // 签退时间
                            {
                                if (checkOutTime == null) // 第一次签退
                                {
                                    checkOutTime = record.getCheckTime();
                                    checkOutStatus = record.getCheckType().toString();
                                }
                                else    // 多次签退
                                {
                                    if (checkOutTime.before(record.getCheckTime()))
                                    {
                                        checkOutTime = record.getCheckTime();
                                        checkOutStatus = record.getCheckType().toString();
                                    }
                                }
                            }

                            // 签到时间和签到状态
                            if (checkInTime != null)
                            {
                                eStatistic.setCheckInStatus(checkInStatus);
                                eStatistic.setCheckInTime(DateUtil.formatToStr(checkInTime, "yyyy-MM-dd HH:mm:ss"));
                                //map.put("checkInStatus", checkInStatus);
                                //map.put("checkInTime", DateUtil.formatToStr(checkInTime, "yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {
                                eStatistic.setCheckInStatus(checkInStatus);
                                //map.put("checkInStatus", checkInStatus);
                                eStatistic.setCheckInTime("未签到");
                                //map.put("checkInTime", "未签到");
                            }

                            // 签退
                            if (checkOutTime != null)
                            {
                                eStatistic.setCheckOutStatus(checkOutStatus);
                                eStatistic.setCheckOutTime(DateUtil.formatToStr(checkOutTime, "yyyy-MM-dd HH:mm:ss"));
                                //map.put("checkOutStatus", checkOutStatus);
                                //map.put("checkOutTime", DateUtil.formatToStr(checkOutTime, "yyyy-MM-dd HH:mm:ss"));
                            }
                            else
                            {
                                eStatistic.setCheckOutStatus(checkOutStatus);
                                eStatistic.setCheckOutTime("未签退");
//                                map.put("checkOutStatus", checkOutStatus);
//                                map.put("checkOutTime", "未签退");
                            }

                            //resultList.add(record);

                        }   // iterate listForStuNoMap end


                        // 通过最后一次刷卡状态码来判断学生是否在校
                        if (StringUtils.equals("0", flagStatus) || StringUtils.equals("1", flagStatus) || StringUtils.equals("2", flagStatus))
                        {
                            eStatistic.setInSchool(true);
                            eStatistic.setCheckOutTime("未签退");
                            eStatistic.setCheckOutStatus("-1");
                        }
                        else
                        {
                            eStatistic.setInSchool(false);
                        }

                        // 判断是否到校过
                        if (!StringUtils.equals("-1", eStatistic.getCheckInStatus()))
                            inSchool++;

                        eStatistic.setStuNo(stuNo);
                        eStatistic.setStuName(stuName);
                        eStatistic.setStuNumber(stuNumber);
                        eStatistic.setClassName(className);
//                        map.put("stuNo", stuNo);
//                        map.put("stuName", stuName);
//                        map.put("stuNumber", stuNumber);
//                        map.put("className", className);
                        //Map aMap = new HashMap(map);
                        resultList.add(eStatistic);

                    }   // iterate stuNoMap end

                }   // iterate checkTimeMap end
            }   // if entrySet != null end
        } // if conditionList != null end


        if (resultList.size() != 0)
        {
            for (EboardStatistic eboardStatistic : resultList)
            {
                if (StringUtils.equals("2", eboardStatistic.getCheckInStatus()))
                {
                    eboardStatistic.setComeLate(true);
                    late++;
                }
                else
                {
                    eboardStatistic.setComeLate(false);
                }

                if (StringUtils.equals("4", eboardStatistic.getCheckOutStatus()))
                {
                    eboardStatistic.setLeaveEarly(true);
                    early++;
                }
                else
                {
                    eboardStatistic.setLeaveEarly(false);
                }


                /*
                if (!StringUtils.equals("-1", (String) map.get("checkInStatus")) //已签到
                        && StringUtils.equals("-1", (String) map.get("checkOutStatus"))) //未签退
                {
                    eboardStatistic.setInSchool(true); // 在校
                    inSchool++;
                }
                else
                {
                    eboardStatistic.setInSchool(false);
                }
                */
            }
        }


        List<EboardStatistic> finalList = new ArrayList<>();
        if (es.getInSchool() != null)
        {
            for (EboardStatistic eboardStatistic : resultList)
            {
                if (es.getInSchool() != null && Objects.equals(es.getInSchool(), eboardStatistic.getInSchool()))
                    finalList.add(eboardStatistic);
            }
        }
        else
        {
            finalList = resultList;
        }

        Map<String, Object> map2 = new HashMap<>();

        Integer studentCount = 0;
        Integer allCount = 0;

        if (classIdStrs != null)
            studentCount = studentDao.queryCountByClassIds(classIdStrs);
        else if (es.getClassId() != null)
        {
            GeneralStudentEntity student = new GeneralStudentEntity();
            student.setClassId(es.getClassId());
            studentCount = studentDao.selectCount(student);
        }

        //TODO 应到人数的处理
        Integer days = DateUtil.getTwoDay(endDate, startDate) + 1;
        allCount = studentCount * days;

        map2.put("comeLateCount", late);
        map2.put("leaveEarlyCount", early);
        map2.put("allCount", allCount);
        map2.put("inSchoolCount", checked);

        resultJson.setData(finalList);
        resultJson.setOtherData(map2);

        return resultJson;
    }

}

